(function() {
	angular
		.module('ydApp.check')
		.config(function($stateProvider) {
			$stateProvider.state('tabs.recent-check', {
				url: '/recent-check',
				nativeTransitions: null,
				cache:false,
				views: {
					'recent-check-tab': {
						controller: "recentCheckCtrl",
						templateUrl: 'views/check/recent-check.html'
					}
				}
			});
			$stateProvider.state('tabs.recent-check-doc', {
				url: '/recent-check-doc-tab',
				nativeTransitions: null,
				views: {
					'recent-check-tab': {
						controller: "recentCheckCtrl",
						templateUrl: 'views/check/recent-check.html'
					}
				}
			});
		})
		.controller("recentCheckCtrl", recentCheckCtrl);

	function recentCheckCtrl($scope, xhr, localStore, $state) {
		localStore.del("editcheck");
		localStore.del('addcheck');

		var vm = $scope.vm = {};

		var userType = localStore.get("userType");

		if (userType == 2) {
			vm.user = localStore.get("user");
			vm.user.branchId = null;
			//0526新加
			vm.user.docId= null;
			//0526新加
			localStore.save("user", vm.user);

			vm.condition = {
				pageIndex: -1,
				sortField: 'createTime',
				sortOrder: 'desc',
				doctorUserId: vm.user.id
			}

			vm.data = [];
			vm.nodata = false;

			vm.search = function() {
				xhr.get('HealthInquiry/com/appraisal/findByCondition', vm.condition).then(function(result) {
					vm.data = result.data;
				});
			};

			vm.doRefresh = function() {
				vm.data = [];
				vm.condition.pageIndex = 0;
				vm.nodata = false;
				vm.load().finally(function() {
					$scope.$broadcast('scroll.refreshComplete');
				});
			}
			vm.loadMore = function() {
				vm.condition.pageIndex++;
				vm.load().then(function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
				});
			}

			vm.load = function() {
				return xhr.get("HealthInquiry/com/appraisal/findByCondition", vm.condition).then(function(result) {
					if (result.data.length == 0) {
						vm.nodata = true;
					}
					vm.data = vm.data.concat(result.data);
				})
			}

			vm.create = function() {
				$state.go("add-check");
			}

			vm.detail = function(id) {
				$state.go("edit-check", {
					id: id
				});
			}
			$scope.delete = function (item) {
				xhr.get("HealthInquiry/com/appraisal/deleteByDoctor", {
					id: item.id,
					doctorUserId: item.doctorUserId
				}).then(function(result) {
					for (var i = 0; i < vm.data.length; i++) {
						if (vm.data[i] == item) {
							vm.data.splice(i, 1);
							break;
						}
					}
				});
			}
		} else {
			vm.user = localStore.get("user");
			vm.user.branchId = null;
			//0526新加
			vm.user.docId= null;
			//0526新加
			localStore.save("user", vm.user);

			vm.condition = {
				pageIndex: -1,
				sortField: 'createTime',
				sortOrder: 'desc',
				userId: vm.user.id
			}

			vm.data = [];
			vm.nodata = false;

			vm.search = function() {
				xhr.get('HealthInquiry/com/appraisal/findByCondition', vm.condition).then(function(result) {
					vm.data = result.data;
				});
			};

			vm.doRefresh = function() {
				vm.data = [];
				vm.condition.pageIndex = 0;
				vm.nodata = false;
				vm.load().finally(function() {
					$scope.$broadcast('scroll.refreshComplete');
				});
			}
			vm.loadMore = function() {
				vm.condition.pageIndex++;
				vm.load().then(function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
				});
			}

			vm.load = function() {
				return xhr.get("HealthInquiry/com/appraisal/findByCondition", vm.condition).then(function(result) {
					if (result.data.length == 0) {
						vm.nodata = true;
					}
					vm.data = vm.data.concat(result.data);
				})
			}

			vm.create = function() {
				$state.go("add-check");
			}

			vm.detail = function(id) {
				$state.go("add-check", {
					id: id
				});
			}
			$scope.delete = function (item) {
				xhr.get("HealthInquiry/com/appraisal/deleteByUser", {
					id: item.id,
					userId: item.userId
				}).then(function(result) {
					for (var i = 0; i < vm.data.length; i++) {
						if (vm.data[i] == item) {
							vm.data.splice(i, 1);
							break;
						}
					}
				});
			}
		}



	}
})();